package org.ne.springboot.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.ne.springboot.service.ImageCheckService;
import org.ne.springboot.service.TableCheckService;
//import org.ne.springboot.utils.PaperHandleUtils.ReportGenerateUtils;
import org.ne.springboot.utils.PaperHandleUtils.PaperContentCheckUtils.ReportGenerateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author luolinila
 * @date 2025/9/2 23:20
 */
@RestController
@RequestMapping(value = "/image")
public class ImageAndTableController {

    @Autowired
    private ImageCheckService imageCheckService;
    @Autowired
    private TableCheckService tableCheckService;

    @PostMapping("/imageAndTableFormat")
    public void imageAndTableFormat(HttpServletResponse response, @RequestParam("file") MultipartFile file) {
        try {
            InputStream is = file.getInputStream();
            XWPFDocument doc = new XWPFDocument(is);
            List<String> imageFormats = imageCheckService.checkImageTitle(doc);
            List<String> tableFormats = tableCheckService.checkTableTitle(doc);

            ReportGenerateUtils.sendReport(response, ReportGenerateUtils.generationImageAndTableFormatReport(imageFormats, tableFormats));

        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostMapping("/imageAndTableContent")
    public void imageAndTableContent(HttpServletResponse response, @RequestParam("file") MultipartFile file) {
        try {
            InputStream is = file.getInputStream();
            XWPFDocument doc = new XWPFDocument(is);
            List<String> imageContent = imageCheckService.checkImageContent(doc);
            List<String> tableContent = tableCheckService.checkTableContent(doc);

            ReportGenerateUtils.sendReport(response, ReportGenerateUtils.generationImageAndTableContentReport(imageContent, tableContent));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
